"""Add score.user_id column

Revision ID: 0c98b865104f
Revises: 7b6a65c708b9
Create Date: 2016-10-27 19:03:44.901639

"""

# revision identifiers, used by Alembic.
revision = '0c98b865104f'
down_revision = '7b6a65c708b9'

from alembic import op
import sqlalchemy as sa
import server


def upgrade():
    op.add_column('score', sa.Column('user_id', sa.Integer(), nullable=True))
    op.create_foreign_key(op.f('fk_score_user_id_user'), 'score', 'user', ['user_id'], ['id'])

    op.execute('''
    UPDATE score AS s
    JOIN backup AS b ON b.id = s.backup_id
    SET s.user_id = b.submitter_id
    ''')

    op.alter_column('score', 'user_id', existing_type=sa.Integer(), nullable=False)

def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(op.f('fk_score_user_id_user'), 'score', type_='foreignkey')
    op.drop_column('score', 'user_id')
    ### end Alembic commands ###
